package com.olddriver.crm.web.controller;

import com.olddriver.crm.annotation.PermissionName;
import com.olddriver.crm.domain.Carproduct;
import com.olddriver.crm.domain.InsuranceList;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.InsuranceCorrectQueryObject;
import com.olddriver.crm.query.InsuranceHistoryQueryObject;
import com.olddriver.crm.query.InsurancePrepareQueryObject;
import com.olddriver.crm.query.InsuranceTSQueryObject;
import com.olddriver.crm.service.IInsuranceListService;
import com.olddriver.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("insuranceTS")
public class InsuranceListController {

    @Autowired
    private IInsuranceListService insuranceListService;

    @RequestMapping("list")
    @RequiresPermissions("insuranceTS:list")
    @PermissionName("查看暂存保单")
    public String list() {
        return "insurance/list";
    }

    @RequestMapping("listCorrect")
    @RequiresPermissions("insuranceTS:listCorrect")
    @PermissionName("查看批改保单")
    public String listCorrect() {
        return "insurance/correctList";
    }

    @RequestMapping("listHistory")
    @RequiresPermissions("insuranceTS:listHistory")
    @PermissionName("查看历史记录")
    public String history() {
        return "insurance/history";
    }

    @RequestMapping("listPrepare")
    @RequiresPermissions("insuranceTS:listPrepare")
    @PermissionName("查看待审核保单")
    public String listPrepare() {
        return "insurance/prepareAudit";
    }


    @RequestMapping("input")
    @RequiresPermissions("insuranceTS:input")
    @PermissionName("查看车险投保")
    public String input() {
        return "insurance/input";
    }




    @ResponseBody
    @RequestMapping("query")
    @RequiresPermissions("insuranceTS:query")
    @PermissionName("保单暂存列表")
    public PageResult query(InsuranceTSQueryObject qo) {
        PageResult result = insuranceListService.query(qo);
        return result;
    }
    @ResponseBody
    @RequestMapping("queryCorrect")
    @RequiresPermissions("insuranceTS:queryCorrect")
    @PermissionName("批改保单列表")
    public PageResult queryCorrect(InsuranceCorrectQueryObject qo) {
        PageResult result = insuranceListService.query(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("queryPrepare")
    @RequiresPermissions("insuranceTS:queryPrepare")
    @PermissionName("审核保单列表")
    public PageResult queryPrepare(InsurancePrepareQueryObject qo) {
        PageResult result = insuranceListService.query(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("queryHistory")
    @RequiresPermissions("insuranceTS:queryHistory")
    @PermissionName("保单历史记录")
    public PageResult queryHistory(InsuranceHistoryQueryObject qo) {
        PageResult result = insuranceListService.query(qo);
        return result;
    }




    @RequestMapping("refuse")
    @ResponseBody
    public JsonResult refuse(Long id) {

        try {
            insuranceListService.refuse(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    @RequestMapping("prepareAudit")
    @ResponseBody
    public JsonResult prepareAudit(Long id) {

        try {
            insuranceListService.prepareAudit(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }
    //审核通过
    @RequestMapping("passAudit")
    @ResponseBody
    public JsonResult passAudit(Long id) {

        try {
            insuranceListService.passAudit(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }
    //退回申请
    @RequestMapping("sendBack")
    @ResponseBody
    public JsonResult sendBack(Long id) {

        try {
            insuranceListService.sendBack(id);
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }


    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(InsuranceList insuranceList, @RequestParam("ids") Long[] ids) {
        try {
            if (insuranceList.getId() != null) {
               insuranceListService.updateByPrimaryKey(insuranceList,ids);
            } else {
                   insuranceListService.insert(insuranceList,ids);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }

    }

    @RequestMapping("getCarproductByInsuId")
    @ResponseBody
    public List<Carproduct> getCarproductByInsuId(Long InsuId){
        return insuranceListService.getCarproductByInsuId(InsuId);
    }


}
